package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("admin/product/test")
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping("testLock")
    public Result testLock(){
        testService.testLock();
        return Result.ok();
    }

    //  读锁
    @GetMapping("read")
    public Result read(){
        String msg =  testService.readLock();
        return Result.ok(msg);
    }

    //  写锁
    @GetMapping("write")
    public Result write(){
        String msg =  testService.writeLock();
        return Result.ok(msg);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //  runAsync方法不支持返回值
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("来了，兄弟，咋看见你呢！");
//        });
//
//        completableFuture.get();

        //  supplyAsync可以支持返回值。
//        CompletableFuture<Integer> completableFutureOne = CompletableFuture.supplyAsync(() -> {
//            System.out.println("有返回值的.... 我们过节了...");
//            //  int i = 1/0;
//            return 1024;
//        }).thenApply((t)->{
//            System.out.println("t----------:"+t);
//            return t*2;
//        }).whenComplete((t,  u)->{
//            //  第一个参数：表示上一个线程的返回值！
//            //  第二个参数：是否有异常信息存在！
//            System.out.println("有返回值的......."+t);
//            System.out.println("有返回值的......."+u);
//        }).exceptionally((t)->{
//            System.out.println("exceptionally......."+t);
//            return 404;
//        });
//        System.out.println(completableFutureOne.get());

        //  定义线程池： 7 个
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                8, //  看cpu 密集型 n+1，还是io 密集型：2*n 核心线程数
                100,
                3L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy() // 如果线程处理不了了，直接抛出异常！
        );

        //  并行！
        CompletableFuture<String> completableFutureA = CompletableFuture.supplyAsync(() -> {
            return "hello";
        },threadPoolExecutor);

        //  创建一个B线程 创建一个C线程
        CompletableFuture<Void> completableFutureB = completableFutureA.thenAcceptAsync((t) -> {
            //  睡眠
            delaySec(3);
            //  打印
            printCurrTime(t+":\t 第一个线程");
        },threadPoolExecutor);
        CompletableFuture<Void> completableFutureC = completableFutureA.thenAcceptAsync((t) -> {
            //  睡眠
            delaySec(1);
            //  打印
            printCurrTime(t+":\t 第二个线程");
        },threadPoolExecutor);

        completableFutureB.get();
        completableFutureC.get();

    }

    private static void printCurrTime(String s) {
        System.out.println(s);
    }

    private static void delaySec(int i) {
        try {
            Thread.sleep(i*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
